package inst

import (
	"errors"
	"math/rand"
)

type RandomBanlance struct {
	remotes []string
	current int
}

func NewRandomBanlance() *RandomBanlance {
	return &RandomBanlance{}
}
func (rb *RandomBanlance) Add(remote string) error {
	if remote == "" {
		return errors.New("remote is empty")
	}
	rb.remotes = append(rb.remotes, remote)
	return nil
}

func (rb *RandomBanlance) Next() string {
	if len(rb.remotes) == 0 {
		return ""
	}
	rb.current = rand.Intn(len(rb.remotes))
	return rb.remotes[rb.current]
}

func (rb *RandomBanlance) Get() (string, error) {
	return rb.Next(), nil
}
